package com.grape.redis.service.impl;

import com.grape.enums.RedisKeyEnums;
import com.grape.redis.service.IDelayQueueHandler;
import com.grape.redis.service.IRedisService;
import com.grape.utils.StringUtils;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 类描述： 二维码超时处理类
 *
 * @author  putao
 * @email   2628279194@qq.com
 */
//@Component
@AllArgsConstructor
public class QrCodeTimeoutHandler implements IDelayQueueHandler<String> {

	private final IRedisService redisService;

	private static final Logger logger = LoggerFactory.getLogger(QrCodeTimeoutHandler.class);

	/**
	 * 处理内容
	 * @param uuid		二维码的uuid前缀
	 */
	@Override
	public void execute(String uuid) {
		logger.error("收到消息并开始处理, 当前时间是:{} ", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.sss").format(new Date()));
		logger.info("收到二维码超时延迟消息：key为{}", uuid);
		String value = redisService.get(RedisKeyEnums.QR_CODE_TIME_OUT.join(uuid));
		if (StringUtils.isNotBlank(value)) {
			logger.error("设置key：{}过期，发送消息给pc", RedisKeyEnums.QR_CODE_TIME_OUT.join(uuid));
			// 存在表示到时了用户尚未扫码后者扫码后尚未授权，这时将二维码改为失效，删除即可或者重置时间为0
			redisService.deleteKey(RedisKeyEnums.QR_CODE_TIME_OUT.join(uuid));
			// TODO 发送消息给前端
//			websocketFeign.sendMessage(WebsocketResult.timeOut(null, uuid));
		} else {
			logger.error("key：{} 不存在，无法执行下面流程", RedisKeyEnums.QR_CODE_TIME_OUT.join(uuid));
		}
	}
}
